﻿//--------------------------------------------------------------------------------------------------------------------------------------------------------
// <copyright file="RegistrationController.cs">
//     Copyright © 2010 Oli Francis
//     This source code is subject to the terms and conditions of the MIT license. A copy of the license can be found in the license.txt 
//     file at the root of this distribution. By using this source code in any fashion, you are agreeing to be bound by the terms of the MIT License. 
//     You must not remove this notice from the software.
// </copyright>
// <author>Oli</author>
//-------------------------------------------------------------------------------------------------------------------------------------------------------

namespace TeamBrain.Web.Controllers
{
    using System;
    using System.Web.Mvc;
    using TeamBrain.Domain.Model;
    using TeamBrain.Domain.Services;
    using TeamBrain.Domain.Services.Exceptions;
    using TeamBrain.Domain.Services.Registration;

    /// <summary>
    /// Controller to handle any requests relating to registration
    /// </summary>
    public class RegistrationController : Controller
    {
        private readonly IRegistrationService registrationService;

        /// <summary>
        /// Initializes a new instance of the <see cref="RegistrationController"/> class.
        /// </summary>
        public RegistrationController(IRegistrationService registrationService)
        {
            this.registrationService = registrationService;
        }

        /// <summary>
        /// Handles a get request for the registration form
        /// </summary>
        [HttpGet]
        public ActionResult Index()
        {
            return View("Index");
        }

        /// <summary>
        /// Handles a post from the registration form
        /// </summary>
        [HttpPost]
        public ActionResult Index(User user, string confirmedPassword)
        {
            if (!user.Password.Equals(confirmedPassword))
            {
                this.ModelState.AddModelError("ConfirmedPassword", "The passwords you entered do not match");
            }

            if (ModelState.IsValid)
            {
                try
                {
                    this.registrationService.RegisterNewUser(user);

                    return RedirectToAction("CheckInbox");
                }
                catch (EmailAlreadyRegisteredException)
                {
                    this.ModelState.AddModelError("EmailAddress", "This email address has already been registered");
                }
            }

            return View("Index");
        }

        /// <summary>
        /// Handles a get request for a page telling the user to check their inbox
        /// </summary>
        [HttpGet]
        public ActionResult CheckInbox()
        {
            return View("CheckInbox");
        }

        /// <summary>
        /// Attempts to activate a user based on a given token
        /// </summary>
        [HttpGet]
        public ActionResult Activate(string token)
        {
            try
            {
                this.registrationService.ActivateUserFromToken(token);
                return RedirectToAction("AccountActivated");
            }
            catch (InvalidActivationTokenException)
            {
            }

            return RedirectToAction("ActivationFailed");
        }

        /// <summary>
        /// Displays an activation failed page
        /// </summary>
        [HttpGet]
        public ActionResult ActivationFailed()
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// Displays an activation confirmation page
        /// </summary>
        [HttpGet]
        public ActionResult AccountActivated()
        {
            throw new NotImplementedException();
        }
    }
}
